
import {
  seckillConfig,
  seckillList
} from "../../../service/jb_seckill_service"
import {
  BIND_USER_ID
} from "../../../service/base_service"
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    menuTop: app.globalData.menuButton.top,
    menuHeight: app.globalData.menuButton.height,
    configList: [],
    productList: [],
    currentIndex: 0,//默认加载第一个秒杀
    currentSeckillId: null,
    seckillPage: 1,
    timeSwap: 0,
    statusName: "进行中",
    isShareEnter: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getSeckillConfig();
    if (options.isShare == 1) {
      this.setData({
        isShareEnter: true,
      })
      if (options.userId) {
        wx.setStorageSync(BIND_USER_ID, options.userId);
      }
    }
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  //获取秒杀配置
  getSeckillConfig() {
    wx.showLoading();
    seckillConfig().then(res => {
      wx.hideLoading();
      if (res.code == 200) {
        this.setData({
          configList: res.data || []
        })
        if (this.data.configList.length > 0) {
          this.setData({
            currentSeckillId: this.data.configList[0].id,
            statusName: this.data.configList[0].statusName
          })
          this.getProductList();
        } else {
          wx.showToast({
            title: '暂无秒杀活动',
            icon: "none"
          })
          setTimeout(() => {
           if(this.data.isShareEnter){
            this.backHomeAction();
           }else{
            wx.navigateBack();
           }
          }, 2000)
        }
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: '加载失败',
        icon: 'none',
      })
    })
  },
  getProductList(loading) {
    if (loading) {
      wx.showLoading();
    }
    seckillList(this.data.currentSeckillId, this.data.seckillPage).then(res => {
      wx.hideLoading();
      if (res.code == 200) {
        this.setData({
          productList: res.data.list || []
        })
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: '加载失败',
        icon: 'none',
      })
    })
  },

  backPageAction() {
    wx.navigateBack();
  },
  
  backHomeAction() {
    wx.switchTab({
      url: '/pages/home/home',
    })
  },

  //切换秒杀
  changeSeckillAction(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      currentSeckillId: this.data.configList[index].id,
      timeSwap: this.data.configList[index].timeSwap,
      currentIndex: index,
      statusName: this.data.configList[index].statusName
    })
    this.getProductList(true);
  },





  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    let userId = app.globalData.userId || ""
    let path = "/featurePackage/pages/partnerArea/parnerArea?isShare=1" + "&userId=" + userId
    return {
      title: app.globalData.commonShareTitle,
      path: path
    }
  }
})